<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      :rules="formRules"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="订单日期" prop="searchTime">
        <el-date-picker
          v-model="queryParams.searchTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="--请选择--" class="!w-200px">
          <el-option label="预约挂号" value="REG"/>
          <el-option label="门诊缴费" value="RECIPE"/>
          <el-option label="住院预交" value="PAYMENT"/>
        </el-select>
      </el-form-item>
      <el-form-item label="门诊号" prop="clinicNo">
        <el-input v-model="queryParams.clinicNo" placeholder="门诊号/住院号" clearable class="!w-200px"/>
      </el-form-item>
      <el-form-item label="平台订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="平台订单号" clearable class="!w-240px"/>
      </el-form-item>
      <el-form-item label="病人姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="病人姓名"
          clearable
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="病人ID" prop="patientId">
        <el-input v-model="queryParams.patientId" placeholder="病人ID" clearable class="!w-200px"/>
      </el-form-item>



      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" border :show-overflow-tooltip="true" :data="list"  style="width: 100%;">
      <el-table-column label="业务类型" align="center" prop="orderType" width="100"/>
      <el-table-column label="病人姓名" align="center" prop="patientName" width="90" />
      <el-table-column label="病人ID" align="center" prop="patientId" width="85" />
      <el-table-column label="门诊号" align="center" prop="clinicNo" width="110" />
      <el-table-column label="平台订单号" align="center" prop="orderNo" width="220" />
      <el-table-column label="支付订单号" align="center" prop="orderId" width="220" />
      <el-table-column label="金额(元)" align="center" prop="cost" width="90" />
      <el-table-column label="订单状态" align="center" prop="payStatus" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="170"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="170" />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="95"
        label="操作">
        <template #default="scope">
          <el-button type="primary" plain size="small" v-if="scope.row.refundStatus === 'unrefund' " @click="refund(scope.row)">退款</el-button>
          <span v-if="scope.row.refundStatus === 'refund'" style="color: red"> 已退款 </span>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as RefundApi from "@/api/hospital/refund";

defineOptions({ name: 'RefundAbnormal' })

const message = useMessage() // 消息弹窗

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = reactive({
  searchTime: [],
  orderType: null,
  patientId: null,
  patientName: null,
  clinicNo: null,
  orderNo: null
})

const formRules = reactive({
  searchTime: [{required: true, message: '订单日期不能为空', trigger: 'blur'}],
  orderType: [{required: true, message: '订单类型不能为空', trigger: 'blur'}],
})

const queryFormRef = ref() // 搜索的表单


/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
}

/** 查询列表 */
const getList = async () => {
  await queryFormRef.value.validate()
  loading.value = true
  try {
    const data = await RefundApi.getOrderList(queryParams)
    console.table(data)
    list.value = data
  } finally {
    loading.value = false
  }
}

const refund  = async (orderInfo) => {
  try {
    await message.confirm(`确认给患者[${orderInfo.patientName}]进行退款[${orderInfo.cost}]元?`, '提示',)
    // 发起退款
    const refundParams = {
      orderType: queryParams.orderType,
      orderNo: orderInfo.orderNo,
      refundFee: orderInfo.cost
    }

    loading.value = true
    try {
      const data = await RefundApi.refundOrder(refundParams)
      message.success("操作成功")
      // 刷新列表
      await getList()
    } finally {
      loading.value = false
    }

  } catch {}
}

</script>

<style scoped>

</style>
